package
{
	import flash.display.DisplayObject;
	
	/**
	 * 字符串工具
	 * @author billowa
	 * 
	 */	
	public class BString
	{
		[Embed(source = "../assets/cn_tw.txt",mimeType="application/octet-stream")]
		private static var datacls:Class;
		private static var datastr:String=String(new datacls());
		
		private static var m_gb2312:String=BString.trim(datastr.substring(0 ,datastr.indexOf('\r\n')));
		private static var m_big5:String=BString.trim(datastr.substring(datastr.indexOf('\r\n')+1 ,datastr.length));
		
		
		/**
		 * 简体转繁体  
		 * @param cc
		 * @return 
		 * 
		 */		
		public static function toTraditionalized(value:String):String
		{
			var str:String = "";
			for (var i:int = 0; i < value.length; i++)
			{
				if (value.charCodeAt(i) > 10000 && m_gb2312.indexOf(value.charAt(i)) != -1)
				{
					str +=  m_big5.charAt(m_gb2312.indexOf(value.charAt(i)));
				}
				else
				{
					str +=  value.charAt(i);
				}
			}
			return str;
		}
		
		
		
		/**
		 * 繁体转简体 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function toSimplized(value:String):String
		{
			var str:String = "";
			for (var i:int = 0; i < value.length; i++)
			{
				if (value.charCodeAt(i) > 10000 && m_big5.indexOf(value.charAt(i)) != -1)
				{
					str +=  m_gb2312.charAt(m_big5.indexOf(value.charAt(i)));
				}
				else
				{
					str +=  value.charAt(i);
				}
			}
			return str;
		}
		
		
		
		/**
		 * 返回指定对象的文件 
		 * @param object 目标对象
		 * @return 
		 * @example <listing version="3.0">
		 trace(BString.getFilePath(this.loaderInfo.content));
		 //返回 项目所在绝对路径;
		 </listing>
		 */		
		public static function getFileName(object:DisplayObject):String
		{
			return (object as DisplayObject).loaderInfo.url.substring(object.loaderInfo.url.lastIndexOf("/")+1,object.loaderInfo.url.length);
		}
		
		
		
		/**
		 * 返回指定对象的本地绝对路径 
		 * @param object 目标对象
		 * @return 
		 * @example <listing version="3.0">
		 trace(BString.getFilePath(this.loaderInfo.content));
		 //返回 项目文件名称;
		 </listing>
		 */		
		public static function getFilePath(object:DisplayObject):String
		{
			return (object as DisplayObject).loaderInfo.url.substring(0,object.loaderInfo.url.lastIndexOf("/")+1);
		}
		
		
		/**
		 * trace(BString.formatString("b{0}l{1}o{2}" ,"i" ,"l" ,"w"));
		 * @param format
		 * @param args
		 * @return 
		 * 
		 */		
		public static function formatString(format:String, ...args):String
		{
			for (var i:int=0; i<args.length; ++i)
				format = format.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
			return format;
		}
		
		
		/**
		 * 去除字符串中非英文类型的字符
		 * @param string 目标字符串
		 * @return 
		 * 
		 */		
		public static function getNotString(string:String):String
		{
			var pattern:RegExp = /[^a-zA-Z]/gi;
			//var pattern:RegExp = /[A-Z]/gi;
			return string.replace(pattern,"");
		}
		
		
		
		
		/**
		 * 是否为英文 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isEnglish(char:String):Boolean
		{
			var pattern:RegExp=/^[A-Za-z]+$/;
			return (pattern.test(char));
		}
		
		
		
		
		
		
		
		/**
		 * 是否为中文 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isChinese(char:String):Boolean
		{
			var pattern:RegExp=/^[\u0391-\uFFE5]+$/;
			return (pattern.test(char));
		}
		
		
		
		
		
		
		/**
		 * 是否为中文或英文
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isChineseEnglish(char:String):Boolean
		{
			var pattern:RegExp=/^[A-Za-z\u0391-\uFFE5]+$/;
			return (pattern.test(char));
		}
		
		
		
		
		
		/**
		 * 是否含有中文字符 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function hasChineseChar(char:String):Boolean
		{
			var pattern:RegExp=/[^\x00-\xff]/;
			return (pattern.test(char));
		}
		
		
		
		
		
		
		/**
		 * 去除字符串左右空格 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function trim(char:String):String
		{
			if (char == null)
			{
				return null;
			}
			return rtrim(ltrim(char));
		}
		
		
		
		/**
		 * 去除字符串  任何空白字符（空格、制表符、换行符或回车符）
		 * @param char
		 * @return 
		 * 
		 */		  
		public static function trimNull(char:String):String
		{
			var pattern:RegExp=/\s/g
			return char.replace(pattern,"");
		}
		
		
		
		
		/**
		 * 去除字符串左空格 
		 * @param char
		 * @return 
		 * 
		 */		  
		public static function ltrim(char:String):String
		{
			var pattern:RegExp = /^\s*/;
			return char.replace(pattern,"");
		}
		
		
		
		/**
		 * 去除字符串右空格 
		 * @param char
		 * @return 
		 * 
		 */		  
		public static function rtrim(char:String):String
		{
			var pattern:RegExp = /\s*$/;
			return char.replace(pattern,"");
		}
		
		
		
		
		
		/**
		 * 是否为双字节 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isDoubleChar(char:String):Boolean
		{
			var pattern:RegExp=/^[^\x00-\xff]+$/;
			return (pattern.test(char));
		}
		
		
		
		
		
		/**
		 * 大小字母比较字符是否相等 
		 * @param char1
		 * @param char2
		 * @return 
		 * 
		 */		
		public static function equalsIgnoreCase(char1:String,char2:String):Boolean
		{
			return char1.toLowerCase() == char2.toLowerCase();
		}
		
		
		
		
		
		/**
		 * 是否为Double型数据 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isDouble(char:String):Boolean
		{
			var pattern:RegExp=/^[-\+]?\d+(\.\d+)?$/;
			return (pattern.test(char));
		}
		
		
		
		/**
		 * 是否为整数 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isInteger(char:String):Boolean
		{
			var pattern:RegExp=/^[-\+]?\d+$/;
			return (pattern.test(char));
		}
		
		
		
		
		/**
		 * 验证字符串指定长度   num1为最小   num2为最大 
		 * @param num1
		 * @param num2
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isLength(min:int,max:int,char:String):Boolean
		{
			return (char.length>=min && char.length<=max);
		}
		
		
		
		
		
		/**
		 * 电话号码验证  格式为：027-88888888 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isPhone(char:String):Boolean
		{
			var pattern:RegExp=/\d{3}-\d{8}|\d{4}-\d{7}/;
			return (pattern.test(char));
		}
		
		
		
		/**
		 * IP验证 格式为：192.168.1.1 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isIP(char:String):Boolean
		{
			var pattern:RegExp=/([0-9]{1,3}\.{1}){3}[0-9]{1,3}/;
			return (pattern.test(char));
		}
		
		
		/**
		 * 验证手机号码 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isMobile(char:String):Boolean
		{
			var pattern:RegExp=/^((\(\d{2,3}\))|(\d{3}\-))?1[3,8,5]{1}\d{9}$/;
			return (pattern.test(char));
		}
		
		
		
		/**
		 * 身份证验证 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isCard(char:String):Boolean
		{
			var pattern:RegExp=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])(\d{4}|\d{3}[Xx])$/;
			return (pattern.test(char));
		}
		
		
		
		
		/**
		 * 是否为Email地址 
		 * @param char
		 * @return
		 * 
		 */		
		public static function isEmail(char:String):Boolean
		{
			var pattern:RegExp=/(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
			return (pattern.test(char));
		}
		
		
		
		
		
		
		/**
		 * 验证是否包含 非英文数据及下划线 之外的字符   ***字符串长度 最少6位  最多30***
		 * @param char
		 * @return 
		 * 
		 */		
		public static function hasAccountChar(char:String):Boolean
		{
			var pattern:RegExp = /^[a-zA-Z][a-zA-Z0-9_]{6,30}$/;
			return pattern.test(char);
		}
		
		
		
		
		
		
		/**
		 * 是否为 URL地址 
		 * @param char
		 * @return 
		 * 
		 */		
		public static function isURL(char:String):Boolean
		{
			var pattern:RegExp=/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
			return (pattern.test(char));
		}
		
		
		
		
		
		/**
		 * 是否为前缀字符串 
		 * @param char
		 * @param prefix
		 * @return 
		 * 
		 */		
		public static function beginsWith(char:String, prefix:String):Boolean{            
			return (prefix == char.substring(0, prefix.length));  
		}  
		
		
		
		
		/**
		 * 是否为后缀字符串 
		 * @param char
		 * @param suffix
		 * @return 
		 * 
		 */		
		public static function endsWith(char:String, suffix:String):Boolean{  
			return (suffix == char.substring(char.length - suffix.length));  
		}  
		
		
		
		
		
		/**
		 * 添加新字符到指定位置 
		 * @param char
		 * @param value
		 * @param position
		 * @return 
		 * 
		 */		         
		public static function addAt(char:String, value:String, position:int):String {  
			if (position > char.length) {  
				position = char.length;  
			}  
			var firstPart:String = char.substring(0, position);  
			var secondPart:String = char.substring(position, char.length);  
			return (firstPart + value + secondPart);  
		}  
		
		
		/**
		 * 替换指定位置字符 
		 * @param char
		 * @param value
		 * @param beginIndex
		 * @param endIndex
		 * @return 
		 * 
		 */		
		public static function replaceAt(char:String, value:String, beginIndex:int, endIndex:int):String {  
			beginIndex = Math.max(beginIndex, 0);             
			endIndex = Math.min(endIndex, char.length);  
			var firstPart:String = char.substr(0, beginIndex);  
			var secondPart:String = char.substr(endIndex, char.length);  
			return (firstPart + value + secondPart);  
		}  
		
		
		
		
		/**
		 * 删除指定位置字符 
		 * @param char
		 * @param beginIndex
		 * @param endIndex
		 * @return 
		 * 
		 */		
		public static function removeAt(char:String, beginIndex:int, endIndex:int):String {  
			return BString.replaceAt(char, "", beginIndex, endIndex);  
		}
	}
}